<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="会员昵称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入会员昵称"
          clearable
        />
      </el-form-item>
      <el-form-item label="会员账号" prop="memberAccount">
        <el-input
          v-model="queryParams.memberAccount"
          placeholder="请输入会员账号"
          clearable
        />
      </el-form-item>
      <!-- <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
        />
      </el-form-item> -->
      <el-form-item label="订单号" prop="ordersno">
        <el-input
          v-model="queryParams.ordersno"
          placeholder="请输入订单号"
          clearable
        />
      </el-form-item>
  
      <el-form-item label="订单日期" prop="advOrdersdate">
        <el-date-picker
          v-model="queryParams.advOrdersdate"
          type="date"
          placeholder="请选择订单日期"
          style="width: 180px"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-tooltip placement="top">
      <div slot="content">点击任意一行即可查看订单明细记录</div>
      
    <el-table
      v-loading="loading"
      :data="roleList"
      @selection-change="handleSelectionChange"
      @row-click="showTab"
      :row-class-name="rowClassName"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="订单Id"
        align="center"
        prop="advOrdersId"
        v-if="false"
      />
      <el-table-column label="订单号" align="center" prop="ordersno" />
      <el-table-column label="订单日期" align="center" prop="advOrdersdate" />
      <el-table-column label="会员昵称" align="center" prop="memberName" />
      <el-table-column label="会员账号" align="center" prop="memberAccount" />
      <!-- <el-table-column label="产品名称" align="center" prop="productName" /> -->
      <el-table-column label="订单总金额" align="center" prop="ordersamount" />
      <!-- <el-table-column label="物流信息" align="center" prop="logisticsInfo" /> -->
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.adv_order_status"
            :value="scope.row.orderStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="关联的硬件订单号" align="center" prop="refOrdersno" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:role:edit']"
            >查看</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:role:remove']"
            >删除</el-button
          > -->
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-user-solid"
            @click="handleSolid(scope.row)"
            v-hasPermi="['system:role:remove']"
          >拉黑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleStop(scope.row)"
            v-hasPermi="['system:role:remove']"
          >禁用</el-button> -->
        </template>
      </el-table-column>
    </el-table>
  </el-tooltip>

    <pagination
    v-show="total>0"
    :total="total"
    :page.sync="queryParams.pageNum"
    :limit.sync="queryParams.pageSize"
    @pagination="getList"/>

    
    <!-- 订单详情 -->
    <div v-if="showCard">
      <!-- 订单详情 -->
      <h2 align="center">订单明细</h2>
      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="12"><el-button plain type="primary" size="mini" icon="el-icon-plus" @click="handleAddByOrder">新增</el-button></el-col> -->
      </el-row>
      <el-table :data="pisOrderList" v-loading="orderloading">
        <el-table-column
          label="产品名称"
          prop="productName"
          align="center"
          width="150"
        ></el-table-column>
        <el-table-column
          label="型号"
          align="center"
          prop="productType"
        ></el-table-column>
        <el-table-column
          label="单价"
          align="center"
          prop="productPrice"
        ></el-table-column>
        <el-table-column
          label="数量"
          align="center"
          prop="ordersNum"
        ></el-table-column>
        <el-table-column
          label="金额"
          align="center"
          prop="ordersamount"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          ———————————
          <!-- <template slot-scope="scope">
                    <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdateByOrder(scope.row)"
                    >修改</el-button>
                    <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDeleteByOrder(scope.row)"
                    >删除</el-button>
                </template> -->
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加或修改用户和角色关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员昵称" prop="memberName" disabled>
          <el-select
            v-model="form.memberName"
            placeholder="订单数量"
            clearable
            @change="memberChange"
            disabled
          >
            <el-option
              v-for="item in memberList"
              :label="item.memberName"
              :value="item.advMembersId"
              :key="item.advMembersId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="会员账号" prop="memberAccount">
          <el-input
            v-model="form.memberAccount"
            placeholder="请输入会员账号"
            clearable
            disabled
          />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-select
            v-model="form.productName"
            placeholder="请输入产品名称"
            clearable
            @change="productChange"
            disabled>
            <el-option
              v-for="item in productList"
              :label="item.productName"
              :value="item.advProductsId"
              :key="item.advProductsId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号" prop="ordersno">
          <el-input
            v-model="form.ordersno"
            placeholder="请输入订单号"
            clearable
            disabled
          />
        </el-form-item>
        <el-form-item label="订单总金额" prop="ordersamount">
          <el-input v-model="form.ordersamount" placeholder="请输入订单总金额" clearable disabled/>
        </el-form-item>
        <el-form-item label="订单日期" prop="advOrdersdate">
          <el-date-picker
            v-model="form.advOrdersdate"
            type="date"
            placeholder="请选择订单日期"
            style="width: 180px"
            value-format="yyyy-MM-dd"
            disabled
          />
        </el-form-item>
        <el-form-item label="关联的硬件订单号" prop="refOrdersno">
          <el-input v-model="form.refOrdersno" placeholder="关联的硬件订单号" clearable disabled/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProducts,
  addProducts,
  updateProducts,
  delProducts,
} from "@/api/server/products.js";
import {
  listMembers,
  addMembers,
  updateMembers,
  delMembers,
} from "@/api/server/members.js";
import {
  listOrders,
  addOrders,
  updateOrders,
  delOrders,
} from "@/api/server/orders.js";
import {
  listOrdersdetail,
  addOrdersdetail,
  updateOrdersdetail,
  delOrdersdetail,
  queryOrderByMoreTwo,
} from "@/api/server/ordersdetail.js";
export default {
  dicts: ["adv_order_status"],
  data() {
    return {
      selectedOrderId: null, // 用于存储被选中的订单ID
      showCard: false,
      orderloading: false,
      pisOrderList: false,
      productList: [],
      memberList: [],
      productType: "2", // 内容产品
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productType: "2", // 内容产品
      },
      showSearch: true,
      single: false,
      multiple: false,
      loading: false,
      currentPage4: 1,
      roleList: [],
      total: 0,
      title: "",
      open: false,
      form: {},
      rules: {
        nickname: [
          { required: true, message: "请输入会员昵称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入会员账号", trigger: "blur" },
        ],
        parentUsername: [
          { required: true, message: "请输入产品名称", trigger: "blur" },
        ],
        appointmentUser: [
          { required: true, message: "请输入订单号", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "请选择数量", trigger: "change" },
        ],
        endTime: [
          { required: true, message: "请选择订单日期", trigger: "change" },
        ],
        status: [
          { required: true, message: "请选择主播状态", trigger: "change" },
        ],
        isAnchor: [
          { required: true, message: "请选择是否主播", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getProductList();
    this.getMemberList();
  },
  methods: {
    // 选择用户发生改变的时候
    memberChange(advMembersId) {
      this.memberList.filter((item) => {
        if (item.advMembersId === advMembersId) {
          this.form.memberAccount = item.memberAccount;
          this.form.advMembersId = item.advMembersId;
        }
      });
    },
    // 选择产品发生改变的时候
    productChange(advProductsId) {
      this.productList.filter((item) => {
        if (item.advProductsId === advProductsId) {
          this.form.advProductsId = item.advProductsId;
          this.form.productName = item.productName;
          this.form.productModel = item.productModel;
          this.form.productDesc = item.productDesc;
          this.form.productPrice = item.productPrice;
          this.form.isSlive = item.isSlive;
          this.form.stocknum = item.stocknum;
        }
      });
    },
    // 查询到所有的产品
    getProductList() {
      listProducts(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
      });
    },
    // 查询所有会员
    getMemberList() {
      listMembers().then((response) => {
        this.memberList = response.rows;
      });
    },
    handleQuery() {
      this.getList();
    },
    resetQuery() {},
    handleExport() {
      this.download('/system/ordersdetail/export', {
        ...this.queryParams
      }, `内容订单列表_${new Date().getTime()}.xlsx`)
    },
    getList() {
      listOrdersdetail(this.queryParams).then((response) => {
        this.roleList = response.rows;
        console.log(response.rows);
      });
    },
    handleSelectionChange() {},
    cancel() {
      this.open = false;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "查看订单";
    },
    rowClassName({ row }) {
    return row.advOrdersId === this.selectedOrderId ? 'selected-row' : ''; // 判断是否选中
    },
    showTab(row) {
      this.orderloading = true;
      this.selectedOrderId = row.advOrdersId; // 存储选中行的订单ID
      queryOrderByMoreTwo(row.advOrdersId).then((response) => {
        this.showCard = true;
        this.orderloading = false;
        this.commloading = false;
        this.pisOrderList = response.rows;
        console.log(response);
      });
    },
    reset() {
      this.form = {
        nickname: "",
        username: "",
        parentUsername: "",
        status: "",
        isAnchor: "",
      };
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该主播, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          row.orderStatus = 4;
          updateOrders(row).then((response) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.title === "添加订单") {
            this.form.balFlag = "0";
            addOrders(this.form).then((response) => {
              this.getList();
              this.$message({
                type: "success",
                message: "提交成功!",
              });
              this.open = false;
            });
          }
          if (this.title === "修改订单") {
            updateOrders(this.form).then((response) => {
              this.getList();
              this.$message({
                type: "success",
                message: "提交成功!",
              });
              this.open = false;
            });
          }
        } else {
          this.$message({
            type: "error",
            message: "表单验证失败!",
          });
        }
      });
    },
    handleSolid(row) {
      this.$confirm("此操作将永久拉黑该主播, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "拉黑成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消拉黑",
          });
        });
    },
    handleStop(row) {
      this.$confirm("此操作将永久禁用该主播, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "禁用成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消禁用",
          });
        });
    },
  },
};
</script>

<style>
.selected-row {
  background-color: #f0f9eb !important; /* 选中行的背景颜色 */
}
</style>

